package com.tengju.support.infrastructure.kafka.producer;

import com.tengju.data.domain.shared.JsonUtil;
import com.tengju.support.domain.model.bizlog.BizLogMessage;
import com.tengju.support.domain.service.log.BizLogProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: 李大仙
 * @Date: 2021/3/9 9:37 下午
 */
@Slf4j
@Component
public class BizLogProducerImpl implements BizLogProducer {

    @Resource(name = "bizLogKafkaTemplate")
    private KafkaTemplate<String, String> kafkaTemplate;

    @Value("${kafka.producer.topic.biz.log}")
    private String topic;

    @Override
    public void sendMessage(BizLogMessage message) {
        String callbackMessage = JsonUtil.toJSONString(message);
        kafkaTemplate.send(topic, callbackMessage).addCallback(
                success -> {},
                failure -> log.error("sendMessage failure, message: {}", callbackMessage));
    }
}